{extend name="common/index"}
{block name="css"}
<style>
    .layui-table-cell{
        padding: 0 8px;
    }
</style>
{/block}
{block name="content"}
<!--条件搜索栏-->
<div class="layui-row">
    <form id="searchForm" class="layui-form layui-form-pane tpl-form-search" onkeydown="if(event.keyCode=='13'){reloadTable('#searchForm');return false;}">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="goods_name" placeholder="标题" class="layui-input">
                </div>
            </div>

            <div id="select-cat-id" class="layui-inline"></div>
            <input type="hidden" name="cat_id" id="cat_id">

            <div class="layui-inline">
                <label class="layui-form-label">推荐等级</label>
                <div class="layui-input-inline">
                    <select name="level">
                        <option value="">请选择</option>
                        {volist name="levelList" id="vo"}
                        <option value="{$vo.basic_id}">{$vo.basic_name}</option>
                        {/volist}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">发布时间</label>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="text" name="public_begin" id="public_begin" class="layui-input">
                </div>
                <div class="layui-form-mid">--</div>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="text" name="public_end" id="public_end" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-normal" onclick="reloadTable('#searchForm');">搜索</a>
                    <a class="layui-btn"  href="{:url('edit')}">新增</a>
                </div>
            </div>
        </div>
    </form>
</div>
<!--数据列表-->
<div class="layui-btn-group">
    <button class="layui-btn layui-btn-primary layui-btn-sm" onclick="ajaxDelete(getSelectedIdList(), '{:url("delete")}');">
        <i class="layui-icon layui-icon-delete"></i>删除选中
    </button>
    <button class="layui-btn layui-btn-primary layui-btn-sm" onclick="setState(getSelectedIdList(), 1, 'state', '{:url("updateSelectedState")}');">
        <i class="layui-icon layui-icon-play"></i>设为发布
    </button>
    <button class="layui-btn layui-btn-primary layui-btn-sm" onclick="setState(getSelectedIdList(), 0, 'state', '{:url("updateSelectedState")}');">
        <i class="layui-icon layui-icon-pause"></i>设为不发布
    </button>
</div>
<table id="goodsTable" lay-filter="goodsTable"></table>
{/block}

{block name="script"}
<script type="text/javascript">
    var table;
    layui.use(['form','laydate', 'table', 'layer'], function () {
        var form = layui.form;
        var laydate = layui.laydate;
        table = layui.table;//全局变量，方便调用删除方法
        var layer = layui.layer;

        /*注册日期，执行一个laydate实例*/
        laydate.render({elem: '#public_begin'});
        laydate.render({elem: '#public_end'});

        //表格数据渲染
        tableIns = table.render({
            elem: '#goodsTable'
            ,url: '{:url("getDataList")}'
            ,page: true
            ,limit: table_limit
            ,limits: table_limits
            ,cols: [[
                {type:'checkbox', width: 45}
                ,{field:'cat_name', title:'类目', width: '8%'}
                ,{field:'goods_name', title:'标题', edit: 'text'}
                ,{field:'years_text', title:'年份', width: '8%'}
                ,{field:'area', title:'产区', width: '8%'}
                ,{field:'level_text', title:'推荐等级', width: '8%', align:'center'}
                ,{field:'read_num', title:'阅读次数', width: '8%', align:'center'}
                ,{title:'更改排序' , width: '8%', align:'center', templet: function (data) {
                    return '<div class="layui-btn-group" title="只在类目内变更顺序">' +
                        '<a class="layui-btn layui-btn-primary layui-btn-xs" onclick="changeSort('+data.goods_id+',\'asc\', \'{:url("changeSort")}\');"><i class="layui-icon layui-icon-up"></i></a>' +
                        '<a class="layui-btn layui-btn-primary layui-btn-xs" onclick="changeSort('+data.goods_id+',\'desc\', \'{:url("changeSort")}\');"><i class="layui-icon layui-icon-down"></i></a>' +
                        '</div>';
                }}
                , {title:'是否发布', width: 90, align:'center', templet: function (data) {
                    var check_state = data.state == 1 ? 'checked' : '';
                    return '<input type="checkbox" name="state" value="'+data.state+'" data-id="'+data.goods_id+'" lay-skin="switch"  lay-text="是|否" lay-filter="statusEvent" '+check_state+'>';
                }}
                ,{title:'操作', width: 135, align:'center', templet: function (data) {
                    var modules = "{$language.module}";
                    var _html = '<div class="layui-btn-group">';
                    if(data.cat_id == 71){
                        var url_view = '{:url("'+modules+'/Goods/roundDetail")}';
                        _html += '<a class="layui-btn layui-btn-normal layui-btn-xs" href="'+url_view+'?id='+data.goods_id+'" target="_blank">预览</a>';
                    }else {
                        var url_view = '{:url("'+modules+'/Goods/detail")}';
                        _html += '<a class="layui-btn layui-btn-normal layui-btn-xs" href="'+url_view+'?id='+data.goods_id+'" target="_blank">预览</a>';
                    }
                    _html += '<a class="layui-btn layui-btn-xs" href="javascript:;" lay-event="edit">编辑</a>';
                    _html += '<a class="layui-btn layui-btn-danger layui-btn-xs" href="javascript:;" lay-event="del">删除</a>';
                    _html += '</div>';
                    return _html;
                }}
            ]]
            ,where: getFormJson('#searchForm')
        });

        //监听工具条
        table.on('tool(goodsTable)', function(obj){
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('数据删除后无法恢复，确定继续吗？', function(index){
                    ajaxDelete(data.goods_id, '{:url("delete")}');
                });
            } else if (obj.event === 'edit') {
                window.location.href = 'edit?goods_id='+data.goods_id;
            }
        });

        //监听状态操作
        form.on('switch(statusEvent)', function(obj){
            var data = {};
            var res = {};
            var index = layer.load(2, {shade:[0.5,'#000'],time: 10*1000});
            data['state'] = obj.elem.checked == true ? 1 : 0;
            data['goods_id'] = $(obj.elem).data('id');

            $.ajax({
                url: '{:url("updateField")}',
                type: 'POST',
                data: data,
                dataType: 'json',
                async: false,
                success: function (result) {
                    layer.close(index);
                    res = result;
                }
            });

            //如果更新失败
            if (!res.code) {
                $(this).prop('checked', !obj.elem.checked);
                form.render('checkbox');// 重新渲染开关
                layer.alert(res.msg, {icon:2});
            }
        });

    });

    //上级类目，级联选择
    layui.config({
        base: '__ADMIN__/js/'
    }).use('linkSelect', function () {
        var linkSelect = layui.linkSelect;
        var inputFrame = 'layui-input-inline';//内联排版

        //url渲染
        link = linkSelect.render({
            elem: '#select-cat-id',
            url: "{:url('GoodsCat/getLinkSelect')}",
            inputFrame: inputFrame,          //默认排版方式
            lableName: '主类目',
            selected: function (item,dom) {
                var val = link.getValue();
                var val_last = 0;
                if (val[val.length-1] != undefined) {
                    val_last = val[val.length-1].value;
                }
                $("#cat_id").val(val_last);
            }
        });
    });

    //获取当前选中的id，数组
    function getSelectedIdList() {
        var checkList = table.checkStatus('goodsTable');
        var id_list = [];
        if (checkList.data.length>0) {
            $.each(checkList.data, function (key, val) {
                id_list[key] = val.goods_id;
            });
        }
        return id_list;
    }

</script>
{/block}